﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wechat.Util.Encryp;
using Wechat.Util.Request;

namespace Wechat.Util.Auth
{
    public class AuthOperaterHelper
    {
        /// <summary>
        ///解密Cookie得到OperaterInfo对象
        /// </summary>
        /// <returns></returns>
        public OperatorCookie DESDecryptCookieInfo(string optCookie)
        {
            if (optCookie != null)
            {
                //解密Cookie得到OperaterInfo对象
                try
                {
                    return JsonConvert.DeserializeObject<OperatorCookie>(EncryptUtil.DESDecrypt(optCookie));
                }
                catch
                {
                    return null;
                }

            }
            return null;
        }

        /// <summary>
        /// 判断用户是否具有访问权限
        /// </summary>
        /// <returns></returns>
        public bool HasPermission(AuthOptRequest authOptRequest, OperatorCookie operCookie)
        {
            var permissionList = string.Empty;//此处为权限sql查询
            //判断为页面权限或者功能权限
            var isPagePermission = authOptRequest.IsAjax ? false : true;
            //permissionList = permissionList.Where(item => item.IsPagePermission == isPagePermission).ToList();

            var accessPath = authOptRequest.AccessPath == "/" ? "/Home/Index" : authOptRequest.AccessPath;
            //忽略大小写
            if (accessPath.IndexOf(permissionList, StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                return true;
            }

            return false;

        }

    }
}
